using System.Web.Mvc;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Models.Binders;

namespace RMH.Web.Controllers
{
    public class RoomController : ControllerBase
    {
        private IRoomService roomService;

        public RoomController(IRoomService roomService)
        {
            this.roomService = roomService;
        }

        //
        // GET: /Room/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(roomService.GetAll());
        }

        //
        // GET: /Room/Details/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int? id)
        {
            return View(roomService.Get(id.Value));
        }

        //
        // GET: /Room/Edit/5
        public ViewResult DropDownList()
        {
            var rooms = roomService.GetAll();
            return View("_DropDownList",rooms);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int? id)
        {
            Room room;
            if (!id.HasValue)
            {
                room = new Room();
            }
            else
            {
                room = roomService.Get(id.Value);
            }
            return View(room);
        }

        //
        // POST: /Room/Edit/5
        [Authorize(Roles = "Administrator")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([ModelBinder(typeof(RoomBinder))] Room room)
        {
            if (room.RoomNumber < 1)
                ModelState.AddModelError("Name", "Room Number must be greater than zero.");
            if (!ModelState.IsValid)
            {
                roomService.Evict(room);
                return View(room);
            }
            roomService.Save(room);
            return RedirectToAction("Index");
        }
    }
}